<?php

include('mysql.php');
include('simple_html_dom.php');
include('seo_url.php');
include('utf8.php');

$db = new MySQL("localhost", "root", "", "crawler");
/*
  function error_handler($errno, $errstr, $errfile, $errline) {
  switch ($errno) {
  case E_NOTICE:
  case E_USER_NOTICE:
  $error = 'Notice';
  break;
  case E_WARNING:
  case E_USER_WARNING:
  $error = 'Warning';
  break;
  case E_ERROR:
  case E_USER_ERROR:
  $error = 'Fatal Error';
  break;
  default:
  $error = 'Unknown';
  break;
  }
  echo '<b>' . $error . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b><br>';
  return true;
  }

  // Error Handler
  set_error_handler('error_handler');
 */
$source = getSourceById(1);
if ($source) {
    $page_url = $source['page_url'];
    $source_id = $source['id'];
    $current_id = $source['current_id'];
    do {
        $link = $page_url . ++$current_id;
        try {
            crawler($source_id, $current_id, $link);
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br>";
            continue;
        }
        //echo $link . "<br>";
    } while ($current_id);
}

function crawler($source_id, $current_id, $link) {
    if (($htmlString = @file_get_contents($link)) === false) {
        return;
    }
    // Dữ liệu trang chủ
    $html = str_get_html($htmlString);
    $title = html_entity_decode($html->find('title', 0)->plaintext, ENT_QUOTES, 'UTF-8');
    $content = $html->find('div#leftColumn', 0);
    if (!$content) {
        return;
    }
    $count_comment = $content->find('span.comments span.number', 0)->plaintext;
    $count_comment = str_replace('.', '', $count_comment);
    if ($count_comment < 500) {
        return;
    }
    $rank_user = $content->find('div.info span.likes', 0)->plaintext;
    $rank_user = str_replace('.', '', $rank_user);
    $image = $content->find('div.photoImg img', 0);
    $image_url = (isset($image->src)) ? $image->src : "";
    if (!empty($image_url)) {
        $data = array("title" => trim($title), "source_id" => $source_id, "image_url" => $image_url, "rank_user" => $rank_user, "count_comment" => $count_comment);
        addImage($data);
    } else {
        $video_url = $content->find('div.photoImg iframe', 0)->src; //->innertext;
        if (!empty($video_url)) {
            $pattern = '/http:\/\/www.youtube.com\/embed\//';
            $replacement = '';
            $video = explode("?", preg_replace($pattern, $replacement, $video_url));
            $youtube_id = $video[0];
            //
            $data = array("title" => trim($title), "source_id" => $source_id, "video_url" => $video_url, "youtube_id" => $youtube_id, "rank_user" => $rank_user, "count_comment" => $count_comment);
            //
            addVideo($data);
        }
    }
    getUpdateSourceById($source_id, $current_id);
}

function getSourceById($id) {
    global $db;
    $query = $db->query("SELECT * FROM crawler_sources WHERE id = '" . (int) $id . "'");
    return $query->row;
}

function getSources() {
    global $db;
    $query = $db->query("SELECT * FROM crawler_sources");
    return $query->rows;
}

function getUpdateSourceById($id, $current_id) {
    global $db;
    $query = $db->query("UPDATE crawler_sources SET current_id = '" . (int) $current_id . "' WHERE id = '" . (int) $id . "'");
}

function addImage($data = array()) {
    global $db;
    if (existImage($data['image_url']))
        return;
    $db->query("INSERT INTO crawler_images SET
        title = '" . $db->escape($data['title']) . "',
        source_id = '" . (int) $data['source_id'] . "',
        image_url = '" . $db->escape($data['image_url']) . "',
        rank_user = '" . (int) $data['rank_user'] . "',
        count_comment = '" . (int) $data['count_comment'] . "'");
}

function addVideo($data = array()) {
    global $db;
    if (existVideo($data['video_url']))
        return;
    $db->query("INSERT INTO crawler_videos SET
        title = '" . $db->escape($data['title']) . "',
        source_id = '" . (int) $data['source_id'] . "',
        video_url = '" . $db->escape($data['video_url']) . "',
        youtube_id = '" . $db->escape($data['youtube_id']) . "',
        rank_user = '" . (int) $data['rank_user'] . "',
        count_comment = '" . (int) $data['count_comment'] . "'");
}

function existImage($image_url) {
    global $db;
    $query = $db->query("SELECT * FROM crawler_images WHERE image_url = '" . $db->escape($image_url) . "'");
    if ($query->num_rows) {
        return true;
    } else {
        return false;
    }
}

function existVideo($video_url) {
    global $db;
    $query = $db->query("SELECT * FROM crawler_videos WHERE video_url = '" . $db->escape($video_url) . "'");
    if ($query->num_rows) {
        return true;
    } else {
        return false;
    }
}

?>
